'use strict';

const { addFollowerFailInfo, deleteFollowerFailInfo } = require("../utils/ErrorModel");
const ErrorModel = require("../utils/ErrorModel");
const { SuccessModel } = require("../utils/resultModel");

module.exports = app => {
  class ProfileController extends app.Controller {
    async index() {
      // 在session中获取用户名
      const { userName } = this.ctx.session.userInfo;
      // 重定向
      await this.ctx.redirect(`/profile/${userName}`);
    }
    // 个人主页
    async profileUser() {
      const { userName: curUserName } = this.ctx.params;
      const { userName, id: userId } = this.ctx.session.userInfo;
      let isMe = true;
      let curUserInfo = {};
      if (curUserName !== userName) {
        isMe = false;
        // 获取当前用户信息前，先判断这个用户是否存在
        const result = await this.service.user.getUserInfo(curUserName);
        if (result) {
          curUserInfo = result;
        }
        else {
          await this.ctx.render('404.html')
          return;
        }
      } else {
        curUserInfo = this.ctx.session.userInfo;
      }
      // 获取博客信息 service
      const result = await this.service.blog.getBlogList({ userName: curUserName, pageSize: 5 });

      // 获取粉丝列表
      const { userList: fansList, count: fansCount } = await this.service.userRelation.getUsersByFollowerId(curUserInfo.id);
      // 获取关注列表
      const { userList: followList, count: followCount } = await this.service.userRelation.getFollower(curUserInfo.id);

      // 是否已经关注此人
      const isMeFollow = fansList.some(item => {
        return item.userName === userName;
      });
      //  获取 at 我的数量
      const atCount = await this.service.atRelation.getAtCount(userId)

      // 最终返回的数据包括 博客信息 用户基本信息
      await this.ctx.render('profile.html', {
        userData: {
          userInfo: curUserInfo,
          fansData: {// 粉丝
            count: fansCount,
            fansList,
          },
          followData: {// 关注
            count: followCount,
            followList,
          },
          isMe,
          isMeFollow,
          atCount,
        },
        blogList: result.blogList,
        pageIndex: 1, // 当前的页面
        pageSize: 5, // 每页显示的数量
        count: result.count, // 微博的总数量
      });
    }
    // 关注
    async follow() {
      // 获取id 获取本人的id 在session中获取
      const { id: myUserId } = this.ctx.session.userInfo;
      // 获取followerId 前端传递过来的被关注人的id
      const { followerId } = this.ctx.request.body;

      try {
        // 调用service
        const result = await this.service.userRelation.addFollower(myUserId, followerId);
        this.ctx.body = new SuccessModel(result);
      } catch (err) {
        this.ctx.body = new ErrorModel(addFollowerFailInfo);
      }
    }
    //取消关注
    async nofollow() {
      // 获取id 获取本人的id 在session中获取
      const { id: myUserId } = this.ctx.session.userInfo;
      // 获取followerId 前端传递过来的被关注人的id
      const { followerId } = this.ctx.request.body;

      try {
        // 调用service
        const result = await this.service.userRelation.noFollower(myUserId, followerId);
        this.ctx.body = new SuccessModel(result);
      } catch (err) {
        this.ctx.body = new ErrorModel(deleteFollowerFailInfo);
      }
    }
  }
  return ProfileController;
};
